# Copyright (C) 2006 Johann Prieur <johann.prieur@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""This module provides a parser to retrieve style properties."""

from xml.sax import make_parser
from xml.sax.handler import ContentHandler

class StylePropertiesParser:
    
    def __init__(self):
        self.parser = make_parser()

    def get_properties(self, file):
        """This method return a dictionary containing properties
           described in the given file.

           This can raise an IOError exception if the parser
           encounters problems trying to load the file"""
        handler = StylePropertiesHandler()
        self.parser.setContentHandler(handler)
        self.parser.parse(open(file))
        return handler.retrieve_dict()
                    
class StylePropertiesHandler(ContentHandler):
    
    def __init__(self):
        self.__dict = {}
        self.isKeyElement = False
        self.isStringElement = False
        self.isIntegerElement = False
        self.isBooleanElement = False

    def startElement(self, element, attributes):
        if element in ['?xml','!DOCTYPE','plist','dict']:
            pass
        elif element == 'key':
            self.isKeyElement = True
        elif element == 'string':
            self.isStringElement = True
        elif element == 'integer':
            self.isIntegerElement = True
        elif element == 'true':
            self.isBooleanElement = True
            self.value = True
        elif element == 'false':
            self.isBooleanElement = True
            self.value == False
        else:
            raise Exception('This style\'s plist is invalid.')

    def characters(self, characters):
        if self.isKeyElement:
            self.current_key = characters
        elif self.isStringElement:
            self.current_value = characters
        elif self.isIntegerElement:
            self.current_value = int(characters)
        elif self.isBooleanElement:
            pass

    def endElement(self, element):
        if element in ['?xml','!DOCTYPE','plist','dict']:
            pass
        elif element == 'key':
            self.isKeyElement = False
        elif element == 'string':
            self.__dict[self.current_key] = self.current_value
            self.isStringElement = False
        elif element == 'integer':
            self.__dict[self.current_key] = self.current_value
            self.isIntegerElement = False
        elif element == 'true':
            self.__dict[self.current_key] = self.current_value
            self.isBooleanElement = False
        elif element == 'false':
            self.__dict[self.current_key] = self.current_value
            self.isBooleanElement = False
        else:
            raise Exception('This style\'s plist is invalid.')

    def retrieve_dict(self):
        return self.__dict

if __name__ == "__main__":
    parser = StylePropertiesParser()
    file = 'styles/GoneDark.AdiumMessageStyle/Contents/Info.plist'
    props = parser.get_properties(file)
    print props
